<?php

namespace common\entities\common;

use common\entities\ConfigEntity;
use common\support\StringHelper;
use Yii;

/**
 * This is the model class for table "myscrm_functionupgrade".
 *
 * @property string $functionUpgradeId
 * @property string $organizationId
 * @property string $functionName
 * @property integer $functionMode
 */
class MyscrmFunctionupgradeEntity extends ConfigEntity
{

    const YDXS_ADV_FLAG = '基础版移动跟客'; // 标记是否购买高级版移动销售的常量
    const IS_NOT_MVP810 = 'NOTMVP810'; // 标记不是mvp810版本
    const IS_CG_QMYX = '项目级客户确认模式'; // 标记是否是重构后的全民营销

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_functionupgrade';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['functionUpgradeId'], 'required'],
            [['functionMode'], 'integer'],
            [['functionUpgradeId', 'organizationId'], 'string', 'max' => 36],
            [['functionName'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'functionUpgradeId' => 'Function Upgrade ID',
            'organizationId' => 'Organization ID',
            'functionName' => 'Function Name',
            'functionMode' => 'Function Mode',
        ];
    }

    /**
     * 判断指定的商户是否有高版本的移动销售
     * @param $orgId
     * @return bool
     */
    public static function isAdvancedVersionYdxs($orgId){
        $fou = self::findOne([
            'organizationId' => $orgId,
            'functionMode' => 2,
            'functionName' => MyscrmFunctionupgradeEntity::YDXS_ADV_FLAG
        ]);
        if(!empty($fou)){
            return false;
        }
        return true;
    }


    /**
     * 判断指定的organization是不是810版本(或其以上)的organization
     * -- 增加是否购买NOTMVP810的灰度
       -- 将目前所有租户的记录，都修改为旧模式（当找到NOTMVP810且functionMode为2时，则为旧模式，否则为新模式）
    DELETE FROM myscrm_functionupgrade where functionName='MPV810';
    DELETE FROM myscrm_functionupgrade where functionName='NOTMVP810';
    INSERT INTO myscrm_functionupgrade (`functionUpgradeId`, `organizationId`, `functionName`, `functionMode`)
    select uuid(),id,'NOTMVP810',2 from myscrm_organization;
     * @param $orgId
     * @return bool
     */
    public static function checkIs810Version($orgId){
        $newOrgs = MyscrmFunctionupgradeEntity::findAll([
            "organizationId" => $orgId,
            "functionName" => MyscrmFunctionupgradeEntity::IS_NOT_MVP810,
            "functionMode" => 2
        ]);
        if(!empty($newOrgs)){
            return false;
        }

        return true;
    }

}
